﻿/* Ext.onReady(function () {
    
    Ext.ns('App');

    App.BTN_OK = 'ok';
    App.BTN_YES = 'yes';
    
    // 1 min. before notifying the user her session will expire. Change this to a reasonable interval.
    App.SESSION_ABOUT_TO_TIMEOUT_PROMT_INTERVAL_IN_MIN = .2;
    
    // 1 min. to kill the session after the user is notified.
    App.GRACE_PERIOD_BEFORE_EXPIRING_SESSION_IN_MIN = 1;

    // The page that kills the server-side session variables.      
    App.SESSION_KILL_URL = '../Home/Logout';

    // Helper that converts minutes to milliseconds.
    App.toMilliseconds = function (minutes) {
        return minutes * 60 * 1000;
    }

    // Helper that simulates AJAX request.
    App.simulateAjaxRequest = function () {

        Ext.Ajax.request({
            url: 'foo.html',
            success: Ext.emptyFn,
            failure: Ext.emptyFn
        });
    }

    // Helper that simulates request to kill server-side session variables.
    App.simulateAjaxRequestToKillServerSession = function () {

        Ext.Ajax.request({
            url: App.SESSION_KILL_URL,
            success: Ext.emptyFn,
            failure: Ext.emptyFn
        });
    }

    // Notifies user that her session is about to time out.
    App.sessionAboutToTimeoutPromptTask = new Ext.util.DelayedTask(function () {

        console.log('sessionAboutToTimeoutPromptTask');

        Ext.Msg.confirm(
            'Your Session is About to Expire', 
            Ext.String.format('Your session will expire in {0} minute(s). Would you like to continue your session?',App.GRACE_PERIOD_BEFORE_EXPIRING_SESSION_IN_MIN),
            function (btn, text) {

                if (btn == App.BTN_YES) {
                    // Simulate resetting the server-side session timeout timer
                    // by sending an AJAX request.
                    App.simulateAjaxRequest();
                } else {
                    // Send request to kill server-side session.
                    App.simulateAjaxRequestToKillServerSession();
                }

            });

        App.killSessionTask.delay(App.toMilliseconds(
          App.GRACE_PERIOD_BEFORE_EXPIRING_SESSION_IN_MIN));
    });

    // Schedules a request to kill server-side session.
    App.killSessionTask = new Ext.util.DelayedTask(function () {
        console.log('killSessionTask');
        App.simulateAjaxRequestToKillServerSession();
    });

    // Starts the session timeout workflow after an AJAX request completes.
    Ext.Ajax.on('requestcomplete', function (conn, response, options) {

        if (options.url !== App.SESSION_KILL_URL) {
            // Reset the client-side session timeout timers.
            // Note that you must not reset if the request was to kill the server-side session.
            App.sessionAboutToTimeoutPromptTask.delay(App.toMilliseconds(App.SESSION_ABOUT_TO_TIMEOUT_PROMT_INTERVAL_IN_MIN));
            App.killSessionTask.cancel();
        } else {
            // Notify user her session timed out.
            Ext.Msg.alert(

                'Session Expired',
                'Your session expired. Please login to start a new session.',

                function (btn, text) {
                    if (btn == App.BTN_OK) {
                        window.location = '../Home/Login';
                    }
                }
            );
        }
    });
});
*/

Ext.Loader.setPath('Ext.ux', '/scripts/ext/lib/ux');
Ext.Loader.setConfig({
    enabled: true
});

Ext.create('Ext.application', {
    appFolder: '/Scripts',
    title: 'App',
    name: 'I2R',
    autoCreateViewport: true,
    controllers: ['HomeController', 'ProposalController', 'UserController'],
    launch: function () {

        var home = this.getController('HomeController');
        var me = this;

        Ext.get("settings-link").on('click', function (e, t) {

            e.preventDefault();

            var win = new Ext.Window({
                title: 'Your profile',
                id: 'my-user-profile',
                iconCls: 'icon_user',
                items: [{xtype: 'i2ruserformpanel', loadId: 0}],
                height: 500,
                width: 800,
                layout: 'fit',
                buttons: [
                    { text: 'Save', iconCls: 'icon_disk', action: 'save-profile' },
                    { text: 'Cancel', iconCls: 'icon_cancel', 
                        handler: function() { 
                            Ext.getCmp('my-user-profile').close(); 
                        }
                    }
                ]
            });

            win.show();

            //            var id = 'tab-id' + me.hash(t.href);
            //            home.addUrlTab(id, t.innerHTML, t.href);

        });


        //        var hash = window.location.hash.substring(1);

        //        if (hash.indexOf('!') >= 0) {
        //            var path = hash.substring(1);
        //            this.getController('HomeController').addActionTab('',path);
        //        }

    },

    hash: function (str) {
        var hash = 0, i, char;

        if (str.length == 0) return hash;

        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
            hash = hash & hash; // Convert to 32bit integer
        }

        return hash;
    }
});